package com.franzliszt.OrderOnline.sunzhengyi.LoginPackage;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.franzliszt.OrderOnline.SQLite.Dao;
import com.franzliszt.OrderOnline.sunzhengyi.custom.PayPwdEditText;
import com.franzliszt.OrderOnline.tool.GoodsList;
import com.franzliszt.OrderOnline.util.KillProcess;
import com.franzliszt.OrderOnline.util.SP;
import com.franzliszt.hospitalnumberingsystem.R;

import java.util.ArrayList;
import java.util.List;

public class UpdatePasswordActivity extends AppCompatActivity {
    private PayPwdEditText PassWordBox;
    private Toast PassWordError;
    private SP sp;
    private Dao dao;
    private List<GoodsList> goodsLists = new ArrayList<>();
    private String UserName,PassWord;
    private TextView Tips,other;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE );
            getWindow().setStatusBarColor( Color.TRANSPARENT );
        }
        setContentView(R.layout.activity_update_password);
        InitView();
        PassWordBox.setOnTextFinishListener( new PayPwdEditText.OnTextFinishListener() {
            @Override
            public void onFinish(String str) {
                String Pay = Searcher(UserName);
                if (str.equals( Pay ))
                {
                    dao.UpdatePassWord(UserName,PassWord);
                    Tips.setText("密码更新成功");
                    PassWordError.show();
                }else {
                    Tips.setText("密码错误");
                    PassWordError.show();
                    PassWordBox.clearText();
                }
            }
        } );
    }
    private void InitView(){
        PassWordBox = findViewById(R.id.PassWordBox);
        PassWordBox.initStyle( R.drawable.bg_paypassword_style,6,3f,R.color.colorWhite,R.color.colorBlack,50);
        sp = SP.getInstance();
        dao = new Dao(UpdatePasswordActivity.this);
        other = findViewById(R.id.exchangeFunc);
        other.setVisibility(View.GONE);
        UserName = (String) sp.GetData(UpdatePasswordActivity.this,"User","");
        PassWord = (String) sp.GetData(UpdatePasswordActivity.this,"PassWord","");
        if (PassWordError == null) {
            PassWordError = new Toast( UpdatePasswordActivity.this );
            View view = LayoutInflater.from( UpdatePasswordActivity.this ).inflate( R.layout.passworderror,null,false );
            Tips = view.findViewById(R.id.ErrorTips);
            PassWordError.setView( view );
            PassWordError.setGravity( Gravity.CENTER,0,0 );
        }
        KillProcess killProcess = KillProcess.getInstance();
        killProcess.addActivity(UpdatePasswordActivity.this);
    }
    private String Searcher(String Phone){
        String Pay = null;
        goodsLists = dao.QueryAll();
        if (goodsLists != null){
            for (int i = 0; i <goodsLists.size() ; i++) {
                if (Phone.equals(goodsLists.get(i).getUserName())){
                    Pay = "000000";
                    return Pay;
                }
            }
        }
        return null;
    }
    public void Exit(View view){
        startActivity(new Intent(UpdatePasswordActivity.this, RegisterADDForget.class));
    }
}
